﻿<template>
    <div style="height:100%;">
        <Row>
            <Col span="24" class="col-margin">
            <!--根据需求删除不不要查询条件-->
            名称：
            <Input v-model="searchModel.RoleName" placeholder="请输入查询的名称值..." style="width: 100px"></Input>
            角色类型：
            <Select v-model="searchModel.RoleType" placeholder="全部" style="width: 100px" @on-change="getPageData">
                <Option value="">全部</Option>
                <Option :value="item.DictValue" v-for="item in roleTypeList" :key="item.DictValue">{{item.DictText}}</Option>
            </Select>
            状态：
            <Select v-model="searchModel.Status" placeholder="全部" style="width: 100px" @on-change="getPageData">
                <Option value="">全部</Option>
                <Option value="1">启用</Option>
                <Option value="0">停用</Option>
            </Select>
            <Button icon="ios-search" type="ghost" @click="getPageData()">查询</Button>
            </Col>
        </Row>
        <Row>
            <Col span="24" class="col-margin">
            <Button icon="plus" type="primary" @click="showType = true">新增</Button>
            <!--<Button icon="android-done-all" type="primary" @click="multiSelect">批量删除</Button>-->
            </Col>
        </Row>
        <Row>
            <Col span="24" class="col-margin">
            <Table size="small" @on-select-all="selectAll" :height="clientHeight" border :context="ctx" :columns="columnsList" :data="datas.rows" no-data-text='暂无数据...'></Table>
            </Col>
        </Row>
        <Row>
            <Col span="24">
            <Page class="page-style" :total="datas.total" placement="top" @on-change="getPageNumber" :page-size="searchModel.PageSize" :page-size-opts='pageSizeOpts' show-sizer @on-page-size-change="getPageSize" show-total show-elevator></Page>
            </Col>
        </Row>
        <Modal v-model="showType" :mask-closable="false" :closable="false">
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">信息编辑</span>
            </p>
            <p slot="close">
                <Icon type="close-circled" color="red" size=25></Icon>
            </p>
            <div slot="footer">
                <Button type="primary" @click="save('dataModel')">提交</Button>
                <Button type="ghost" @click="cancel('dataModel')" style="margin-left: 8px">取消</Button>
            </div>
            <Form ref="dataModel" :model="dataModel" :rules="ruleValidate" :label-width="80">
                <Form-item style="display:none;" prop="Id">
                    <Input v-model="dataModel.Id"></Input>
                </Form-item>
                <Form-item label="名称" prop="RoleName">
                    <Input v-model="dataModel.RoleName" placeholder="请输入名称"></Input>
                </Form-item>
                <Form-item label="角色类型" prop="RoleType">
                    <Select v-model="dataModel.RoleType" placeholder="请选择角色类型">
                        <Option :value="item.DictValue" v-for="item in roleTypeList" :key="item.DictValue">{{item.DictText}}</Option>
                    </Select>
                </Form-item>
                <Form-item label="状态" prop="Status">
                    <Select v-model="dataModel.Status" placeholder="请选择状态">
                        <Option value="1">启用</Option>
                        <Option value="0">停用</Option>
                    </Select>
                </Form-item>
                <Form-item label="排序" prop="Sort">
                    <Input v-model="dataModel.Sort" placeholder="请输入排序"></Input>
                </Form-item>
                <Form-item label="备注" prop="Remark">
                    <Input v-model="dataModel.Remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注长度请小于150个字符..."></Input>
                </Form-item>
            </Form>
        </Modal>
        <Modal v-model="showMenuType" :mask-closable="false" width="800">
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">角色权限管理</span>
            </p>
            <p slot="close">
                <Icon type="close-circled" color="red" size=25></Icon>
            </p>
            <div slot="footer">
                <Button type="primary" @click="RMSave('RoleMenuModel')">提交</Button>
                <Button type="ghost" @click="RMCancel('RoleMenuModel')" style="margin-left: 8px">取消</Button>
            </div>
            <Row>
                <Col span="10">
                <div style="text-align: center;font-size:18px;">
                    <B>功能列表</B>
                </div>
                <div style="height:600px;overflow-y:auto;border: 1px solid #F0F0F0;padding: 10px;">
                    <Tree ref="tree" :data="menuTreeData" :check-strictly="true" show-checkbox @on-select-change="treeSelctChange"></Tree>
                </div>
                </Col>
                <Col span="14">
                <Row style="padding:2px 0 0 15px;">
                    <Col style="font-size:18px;">
                    <B>操作列表</B>
                    </Col>
                </Row>
                <Row style="padding:2px 0px 5px 15px;">
                    <Col>
                    <Table :columns="actionColumList" :data="menuActionData" size="small" @on-selection-change="selecChange" @on-select-all="selectAll"></Table>
                    <!--{{MenuRoleList}}
                        <br/> {{MenuRoleActionList}}
                        <br/> {{MRIdList}}-->
                    </Col>
                </Row>
                </Col>
            </Row>
        </Modal>
    </div>
</template>
<script>
export default {
    data() {
        return {
            RoleId: '',//对应的角色编号
            MenuId: '',//对应的操作权限所属菜单ID
            MRIdList: [],//全部权限：全部所选权限列表{角色编号、菜单编号、操作编号}
            MenuRoleList: [],//菜单权限：选择菜单保存{角色编号、菜单编号、''}
            MenuRoleActionList: [],//操作权限：选择菜单保存{角色编号、菜单编号、操作编号}
            menuActionData: [],//菜单操作列表
            menuTreeData: [],//菜单树数据
            showType: false,//编辑框弹出状态
            showMenuType: false,//角色权限管理
            RoleMenuModel: [],//角色菜单权限
            ruleValidate: {//验证模型
                RoleName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
                RoleType: [{ required: true, message: '角色类型不能为空', trigger: 'blur' }],
                Status: [{ required: false, message: '状态不能为空', trigger: 'blur' }],
                Sort: [{ required: false, message: '排序不能为空', trigger: 'blur' }],
                Remark: [
                    { required: false, message: '备注长度请小于100个字符', trigger: 'blur' },
                    { type: 'string', max: 100, message: '备注长度请小于150个字符', trigger: 'blur' }
                ]
            },
            dataModel: {//编辑模型
                Id: 0,
                RoleName: '',
                RoleType: '1',
                Status: '1',
                Sort: '1',
                Remark: ''
            },
            //查询模型：根据需求删除不不要查询条件
            searchModel: {
                Id: '',
                RoleName: '',
                RoleType: '',
                Status: '',
                Sort: '',
                Remark: '',
                PageNumber: 1, PageSize: 20
            },
            roleTypeList: this.GetGroupDict("RoleType"),
            pageSizeOpts: [20, 40, 50],//默认分页最大设置
            datas: {},//table数据对象
            dataIds: [],
            ctx: this,//table数据上下文
            actionColumList: [//表头对象
                { type: 'selection', width: 60, align: 'center' },//多选设置
                { title: "操作地址", key: "CtrlUrl" },
                { title: "操作名称", key: "ActionName" },
                { title: "代码", key: "ActionCode" }
            ],
            columnsList: [//表头对象
                // { type: 'selection', width: 60, align: 'center' },//多选设置
                { title: "名称", key: "RoleName", width: 200 },
                { title: "角色类型", key: "RoleTypeName", width: 100 },
                { title: "状态", key: "StatusName", width: 80 },
                { title: "排序", key: "Sort", width: 80 },
                { title: "备注", key: "Remark" },
                {
                    title: "操作", key: "Id", width: 250, align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                class:{
                                    ieditColor:true
                                },
                                props: {
                                    size: 'small',
                                    type:'ghost',
                                    icon: 'pull-request'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.ShowMenuTreeModal(params.row.Id)
                                    }
                                }
                            }, '授权'),
                            h('Button', {
                                class:{
                                    ieditColor:true
                                },
                                props: {
                                    size: 'small',
                                    type:'ghost',
                                    icon: 'ios-compose'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.editInfo(params.row.Id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                class:{
                                    ideleteColor:true
                                },
                                props: {
                                    size: 'small',
                                    type:'ghost',
                                    icon: 'ios-trash-outline'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.delInfo(params.row.Id)
                                    }
                                }
                            }, '删除'),
                        ]);
                    }
                }]
            , clientHeight: window.innerHeight - 260,//表格默认高度
        }
    },
    mounted() {
        //table高度计算方法
        const that = this;
        window.onresize = () => {
            return (() => {
                that.clientHeight = window.innerHeight - 260;
            })()
        };
    },
    methods: {
        //获取当前页码
        getPageNumber(pageIndex) {
            this.searchModel.PageNumber = pageIndex;
            this.getPageData();
        },
        //获取当前分页大小
        getPageSize(pageSize) {
            this.searchModel.PageSize = pageSize;
            this.getPageData();
        },
        //获取this.datas: {},//table数据对象
        getPageData() {
            var self = this;
            self.$http.post('Role/PostPageList', this.searchModel).then(function (res) {
                if (res.data.status === 1) {
                    self.datas = res.data;
                }
                else {
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },
        //编辑获取对象传入id
        editInfo(id) {//编辑
            var self = this;
            this.$http.get("Role", { params: { id: id } }).then(//获取数据编辑项的值
                function (res) {
                    if (res.data.status == 1) {
                        self.dataModel = res.data.model;
                        self.modalShow();//打开modal
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "编辑数据加载失败!!!" });
                    }
                }
            );
        },
        //删除对象
        delInfo(id) {
            var self = this;
            self.$Modal.confirm({
                title: '提示信息',
                content: '<p>数据删除后不可恢复，确定要删除？</p>',
                onOk: () => {
                    this.$http.delete("Role", { params: { id: id } }).then(
                        function (res) {
                            if (res.data.status == 1) {
                                self.$Notice.success({ title: '删除成功！', desc: false });
                                self.getPageData();
                            }
                            else {
                                self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                            }
                        }
                    );
                }
            });
        },
        //保存方法
        save(modelname) {
            var self = this;
            this.$refs[modelname].validate((valid) => {
                if (valid) {
                    if (this.dataModel.Id === 0) {
                        this.$http.post('Role', this.dataModel).then(
                            function (res) {
                                switch (res.data.status) {
                                    case 0:
                                        self.$Modal.info({ title: "提示信息", content: res.data.message, desc: false });
                                        break;
                                    case 1:
                                        self.getPageData();//读取分页数据
                                        self.restModel(modelname);//重置model
                                        self.modalHide();//关闭modal
                                        self.$Notice.success({ title: '保存成功！', desc: false });
                                        break;
                                    case -1:
                                        self.$Modal.warning({ title: "提示信息", content: "修改失败,请与管理员联系!" });
                                        break;
                                }
                            });
                    } else {
                        this.$http.put('Role', this.dataModel).then(
                            function (res) {
                                switch (res.data.status) {
                                    case 0:
                                        self.$Modal.info({ title: "提示信息", content: res.data.message, desc: false });
                                        break;
                                    case 1:
                                        self.getPageData();//读取分页数据
                                        self.restModel(modelname);//重置model
                                        self.modalHide();//关闭modal
                                        self.$Notice.success({ title: '保存成功！', desc: false });
                                        break;
                                    case -1:
                                        self.$Modal.warning({ title: "提示信息", content: "修改失败,请与管理员联系!" });
                                        break;
                                }
                            });
                    }
                }
            });
        },
        //编辑框打开
        modalShow() {
            this.showType = true;
        },
        //编辑框关闭
        modalHide() {
            this.showType = false;
        },
        //取消方法
        cancel(modelname) {
            this.$refs[modelname].resetFields();//重置model
            this.showType = false;//关闭modal
        },
        //重置model
        restModel(modelname) {
            this.$refs[modelname].resetFields();//重置model
        },
        //初始化菜单树
        ShowMenuTreeModal(id) {
            this.showMenuType = true;
            this.RoleId = id;
            var self = this;
            this.$http.get("role/GetMRTreeList", { params: { id: id } })
                .then(function (res) {
                    if (res.data.status == 1) {
                        self.menuTreeData = res.data.model;
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                    }
                });
            this.$http.get("MenuRole/GetList", { params: { id: id } })
                .then(function (res) {
                    if (res.data.status == 1) {
                        self.MenuRoleActionList = res.data.model;
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                    }
                });
        },
        //保存角色权限列表
        RMSave() {
            var self = this;
            var treeNodeList = self.$refs.tree.getCheckedNodes();
            this.treeCheckChange(treeNodeList);
            console.log(this.MenuRoleList);
            this.MRIdList = this.MenuRoleList;
            //删除操作权限
            this.DelActionList();
            //添加操作权限
            for (var i = 0; i < this.MenuRoleActionList.length; i++) {
                this.MRIdList.push(this.MenuRoleActionList[i]);
            }
            var model = new Object();
            model.RoleId = this.RoleId;
            model.MRList = this.MRIdList;
            this.$http.post("MenuRole/Post", model)
                .then(function (res) {
                    if (res.data.status == 1) {
                        self.RMCancel();
                        self.$Notice.success({ title: '授权成功！', desc: false });
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "授权失败!" });
                    }
                });
            this.showMenuType = false;
        },
        //删除全部所选权限列表里面ActionId不为空的
        DelActionList() {
            for (var i = 0; i < this.MRIdList.length; i++) {
                var obj = this.MRIdList[i];
                if (obj.ActionId != "") {
                    this.MRIdList.splice(i, 1);
                    i--;
                }
            }
        },
        //授权modal关闭
        RMCancel() {//授权modal关闭
            self.menuTreeData = [];
            this.showMenuType = false;
            this.RoleId = "";
            this.MenuId = "";
            this.MRIdList = [];
            this.MenuRoleList = [];
            this.MenuRoleActionList = [];
            this.menuActionData = [];
        },
        //全选方法
        selectAll(selection) {
            this.getRoleMenuActionIdList(selection);
        },
        //checkbox取消选中方法
        selecChange(selection) {
            this.getRoleMenuActionIdList(selection);
        },
        //设置对应的操作权限
        getRoleMenuActionIdList(selection) {
            //删除指定菜单的操作权限
            this.DelMenuList(this.MenuId);
            if (selection.length !== 0) {
                for (var i = 0; i < selection.length; i++) {
                    //增加操作权限
                    this.MenuRoleActionList.push({ RoleId: this.RoleId, MenuId: selection[i].MenuId, ActionId: selection[i].Id });
                }
            }
        },
        //删除对应菜单的操作权限
        DelMenuList(menuid) {
            for (var i = 0; i < this.MenuRoleActionList.length; i++) {
                var obj = this.MenuRoleActionList[i];
                if (obj.MenuId == menuid) {
                    this.MenuRoleActionList.splice(i, 1);
                    i--;
                }

            }
        },
        //树的checkbox选择事件
        treeCheckChange(obj) {
            this.MenuRoleList = [];
            if (obj.length !== 0) {
                for (var i = 0; i < obj.length; i++) {
                    this.MenuRoleList.push({ RoleId: this.RoleId, MenuId: obj[i].Id, ActionId: "" });
                    if (obj[i].ParentId != "") {
                        //检查父级是否存在,不存在则新增
                        if (!this.CheckParentMenu(obj[i].ParentId)) {
                            this.MenuRoleList.push({ RoleId: this.RoleId, MenuId: obj[i].ParentId, ActionId: "" });
                        }
                    }
                }
            } else {
                console.log(obj);
                this.MenuRoleList = [];
            }
        },
        //检查父级是否存在
        CheckParentMenu(parentid) {
            for (var i = 0; i < this.MenuRoleList.length; i++) {
                if (parentid == this.MenuRoleList[i].MenuId && parentid != "") {
                    return true;
                }
            }
        },
        //获取操作权限列表
        treeSelctChange(obj) {
            var self = this;
            //设置当前的菜单ID
            this.MenuId = obj[0].Id;
            this.$http.get("MenuAction/GetList", { params: { id: this.MenuId } })
                .then(function (res) {
                    if (res.data.status == 1) {
                        self.menuActionData = res.data.model;
                        for (var i = 0; i < self.menuActionData.length; i++) {
                            //判定是否存在，存在选中，不存在不选中
                            if (self.CheckMenuAction(self.menuActionData[i].MenuId, self.menuActionData[i].Id)) {
                                //查看iview的table api
                                self.menuActionData[i]._checked = true;
                            } else {
                                self.menuActionData[i]._checked = false;
                            }
                        }
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                    }
                });
        },
        //验证操作权限是否选中
        CheckMenuAction(menuid, actionid) {
            for (var i = 0; i < this.MenuRoleActionList.length; i++) {
                var obj = this.MenuRoleActionList[i];
                if (obj.MenuId == menuid && obj.ActionId == actionid) {
                    return true;
                }
            }
        }
    },
    //创建Vue页面方法
    created: function () {
        //创业Vue前调用获取this.datas: {},//table数据对象
        this.getPageData();
    },
    watch: {
        //需要监听的在此处新增监听对象
    }
}
</script>
